package de.tuhh.simsearch.util;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import de.tuhh.simsearch.constants.Constants;
import de.tuhh.simsearch.dto.CategoryDTO;
import de.tuhh.simsearch.dto.FileDTO;
import de.tuhh.simsearch.model.persistence.contentmgmt.eao.interfaces.ContentMgmtEAOInterface;
import de.tuhh.simsearch.model.persistence.contentmgmt.entities.CategoryEntity;
import de.tuhh.simsearch.model.persistence.contentmgmt.entities.FileEntity;

/**
 * The ContentConversion class provides functionality to convert content entity objects into data transfer objects and vice versa.
 * The content entity objects are file entities and category entities.
 * 
 * The data transfer objects are used for transmitting necessary information about files and categories to the client.
 *
 * @author SimSearch 1
 * 
 */
@Stateless
public class ContentConversion {

  /**
   * The database connection
   */
  @EJB private ContentMgmtEAOInterface conMmgmt;

  /**
   * Converts a file entity object into a file dto
   * 
   * @param entity
   *          the entity object to convert
   * @return the resulting dto
   */
  public FileDTO fromEntity(FileEntity entity) {

    FileDTO dto = new FileDTO(entity.getId());
    Set<String> categories = new HashSet<String>();

    if (entity.getCategories() != null) {
      for (CategoryEntity e : entity.getCategories()) {
        categories.add(e.getCategoryName());
      }
    }

    dto.setCategories(categories);
    dto.setDisplayedFilename(entity.getDisplayedFilename());
    dto.setContentType(Constants.contentIdDescMap.get(entity.getContentID()));
    dto.setOwner(entity.getOwner());
    dto.setDescription(entity.getDescription());

    return dto;
  }

  /**
   * Converts a category entity into a category dto
   * 
   * @param entity
   *          the entity to convert
   * @return the resulting dto
   */
  public CategoryDTO fromEntity(CategoryEntity entity) {

    CategoryDTO cDto = new CategoryDTO(Constants.contentIdDescMap.get(entity.getContentID()), entity.getCategoryName());
    Set<BigInteger> files = new HashSet<BigInteger>();

    Set<FileEntity> fEntities = entity.getFiles();
    if (fEntities != null) {
      for (FileEntity e : fEntities) {
        files.add(e.getId());
      }
    }

    cDto.setFiles(files);

    return cDto;
  }

  /**
   * Converts a category dto into an entity
   * 
   * @param dto
   *          the dto to be converted
   * @return the resulting entity object
   */
  public CategoryEntity fromDto(CategoryDTO dto) {

    Integer contId = Constants.descContentIdMap.get(dto.getContentType());
    CategoryEntity cEntity = null;

    if (contId != null && dto.getCategoryName() != null) {

      cEntity = conMmgmt.getCategoryEntity(dto.getCategoryName(), contId);

      if (cEntity == null) {
        cEntity = new CategoryEntity(contId, dto.getCategoryName());
      }

      if (dto.getFiles() != null && !dto.getFiles().isEmpty()) {
        for (BigInteger fid : dto.getFiles()) {
          try {
            cEntity.addFile(conMmgmt.getFileEntity(fid));
          } catch (NullPointerException npe) {
            System.out.println("Could not add FileEntity to CategoryEntity");
            npe.printStackTrace();
          }
        }
      }
    }

    return cEntity;
  }

}
